<template>
    <a-locale-provider :locale="zh_CN">
      <router-view></router-view>
    </a-locale-provider>
</template>
<script>



    import persistentUtil from './common/persistentUtil'

    import Vue from 'vue'
    import './plugins/axios'

    import event from './plugins/event'

    import router from './plugins/router'

    import PageHeader from './components/PageHeaderComponent'

    import SystemDictDisplay from './components/dict/SystemDictDisplay'

    import SystemDictData from './components/dict/SystemDictData'

    Vue.component("PageHeader", PageHeader);
    Vue.component("SystemDictDisplay",SystemDictDisplay);
    Vue.component("SystemDictData",SystemDictData);

    Vue.config.productionTip = false

    import './common/initVar'

    import btnPermission from './components/Permission'

    Vue.use(btnPermission);

    import {LocaleProvider,message} from 'ant-design-vue';

    import zh_CN from 'ant-design-vue/lib/locale-provider/zh_CN'

    export default {
        name: 'SDSApp',
        components:{
            ALocaleProvider:LocaleProvider
        },

        props:{
            bussinessRouter:{
                type:Array,
                default:()=>{
                    return [];
                }
            }
        },

        router: router,
        data() {
            return {
                zh_CN,
                loadingDictIdent:[]
            }
        },
        created(){

          if(Array.isArray(this.bussinessRouter)){

              //抽取登录组件
              if(Vue.prototype.$loginPath != undefined){

                  for(let index = 0 ;index < this.bussinessRouter.length;index++){
                      let br = this.bussinessRouter[index];
                      if(br.path === Vue.prototype.$loginPath){
                          this.bussinessRouter.splice(index,1);

                          this.$router.options.routes.push(br);

                          this.$router.addRoutes(this.$router.options.routes);
                          break;
                      }
                  }

              }

              //处理动态子路由
              for(let i = 0; i < this.$router.options.routes.length;i++){

                  if(this.$router.options.routes[i].path === '/'){

                      if(this.$router.options.routes[i].children){

                          this.$router.options.routes[i].children = this.$router.options.routes[i].children.concat(this.bussinessRouter);

                          this.$router.addRoutes(this.$router.options.routes);

                          console.log("添加动态路由完毕");

                          break;
                      }

                  }else if(this.$router.options.routes[i].path === '/login'){

                  }
              }

          }
        },

        mounted(){
            //绑定字典加载事件
            event.$on("loadRemoteDict",this.loadRemoteDict)

            //如果当前没有登录就跳转到登录 否则就是刷新操作
            let token = persistentUtil.getToken();

            if(token == null || token == undefined){

                this.$router.push("/login")
            }
        },
        destroyed(){
            //解除绑定字典加载事件
            event.$off("loadRemoteDict",this.loadRemoteDict)
        },

        methods:{

            /**
             * 加载远程字典
             * @param dictIdent
             */
            loadRemoteDict(dictIdent){

                if(this.loadingDictIdent.indexOf(dictIdent) > -1){

                    return;
                }

                this.loadingDictIdent.push(dictIdent);

                this.$axios.get("/auth/dict/getDictItemListByDictIdent?dictIdent=" + dictIdent)
                    .then(d=>{

                        if(d.data.status === 0){

                            this.dictItems = d.data.data;

                            persistentUtil.saveDictItems(dictIdent,d.data.data);

                            event.$emit("loadRemoteDictFinish",dictIdent)

                        }
                        this.loadingDictIdent.splice(this.loadingDictIdent.indexOf(dictIdent),1)
                    },e=>{

                        this.loadingDictIdent.splice(this.loadingDictIdent.indexOf(dictIdent),1)
                        //
                    })

            }

        }
    }
</script>

<style>
    #app {
        font-family: 'Avenir', Helvetica, Arial, sans-serif;

        height: 100vh;

    }
</style>
